package com.flu.kafka.base1.doc;

import com.flu.kafka.doc.ApiDocInfoProperties;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(ApiDocInfoProperties.class)
public class OpenApiConfig {


    /**
     * API 文档信息属性
     */
    private final ApiDocInfoProperties apiDocInfoProperties;

    @Bean
    public GroupedOpenApi tweetsOpenApi() {
        String[] paths = {"/history/**"};
        return GroupedOpenApi.builder().
                group("history")
                .addOpenApiCustomizer(openApi -> openApi.info(new Info().title("history Api").version(apiDocInfoProperties.getVersion())))
                .pathsToMatch(paths)
                .build();
    }

    @Bean
    public GroupedOpenApi streamOpenApi() {
        String[] paths = {"/ai/**"};
        String[] packagedToMatch = {"com.flu.ai"};
        return GroupedOpenApi.builder().group("x-stream")
                .addOpenApiCustomizer(openApi -> openApi.info(new Info().title("ai API").version(apiDocInfoProperties.getVersion())))
                .pathsToMatch(paths).packagesToScan(packagedToMatch)
                .build();
    }
    /**
     * OpenAPI 配置（元信息、安全协议）
     */
    //@Bean
    public OpenAPI apiInfo() {
        return new OpenAPI()
                .info(new Info()
                        .title(apiDocInfoProperties.getTitle())
                        .version(apiDocInfoProperties.getVersion())
                        .description(apiDocInfoProperties.getDescription())
                        .contact(new Contact()
                                .name(apiDocInfoProperties.getContact().getName())
                                .url(apiDocInfoProperties.getContact().getUrl())
                                .email(apiDocInfoProperties.getContact().getEmail())
                        )
                        .license(new License().name(apiDocInfoProperties.getLicense().getName())
                                .url(apiDocInfoProperties.getLicense().getUrl())
                        ));
    }

}
